package com.glowdayz.glowmee;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.glowdayz.common.ActivityManager;
import com.glowdayz.common.CDialog;
import com.glowdayz.common.HttpHandler.FileInfo;
import com.glowdayz.common.ImageDownloader;
import com.glowdayz.common.XmlParser;
import com.glowdayz.model.AddItem;
import com.glowdayz.model.PouchCMD;
import com.glowdayz.model.PouchProductCMD;
import com.glowdayz.model.ProductCMD;
import com.glowdayz.model.UserInfo;

public class AddPouch extends Activity implements OnClickListener, OnItemSelectedListener {
	
	private ActivityManager am = ActivityManager.getInstance();
	
	Spinner pouchCategory = null;
	String[] categoryArr = null;
	String categoryText = "", url = "", pouchTitleValue = "", pouchTextValue = "";
	
	ImageView productImg1 = null, productImg2 = null, productImg3 = null, productImg4 = null, productImg5 = null,
			productImg6 = null, productImg7 = null, productImg8 = null, productImg9 = null, productImg10 = null;
	ImageView updateImg1 = null, updateImg2 = null, updateImg3 = null, updateImg4 = null, updateImg5 = null,
			updateImg6 = null, updateImg7 = null, updateImg8 = null, updateImg9 = null, updateImg10 = null;
	ImageView deleteImg1 = null, deleteImg2 = null, deleteImg3 = null, deleteImg4 = null, deleteImg5 = null,
			deleteImg6 = null, deleteImg7 = null, deleteImg8 = null, deleteImg9 = null, deleteImg10 = null;
	
	FileInfo fileInfo1 = null, fileInfo2 = null, fileInfo3 = null, fileInfo4 = null, fileInfo5 = null,
			fileInfo6 = null, fileInfo7 = null, fileInfo8 = null, fileInfo9 = null, fileInfo10 = null;
	
	String t1 = "", t2 = "", t3 = "", t4 = "", t5 = "", t6 = "", t7 = "", t8 = "", t9 = "", t10 = "";
	
	int[] productArr = new int[10]; 
	
	EditText pouchTitle = null, pouchText = null;
	
	ImageView okBt = null;
	
	AlertDialog.Builder alert = null;
	
	int addIdPouch = 0;
	
	PouchCMD pouchItem = null;
	
	ImageView product_selection = null;
	
	int pouchCount = 0;
	int refreshCount = 1;
	
	String categoryUrl = null;
	String imgUrl = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_pouch);
		
		am.addActivity(this);
		
		ImageView backBt = (ImageView) findViewById(R.id.backBt);
		backBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		backBt.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		TextView textTitle = (TextView) findViewById(R.id.textTitle);
		textTitle.setText("픽 만들기");
		
		pouchCategory = (Spinner) findViewById(R.id.pouchCategory);
		pouchCategory.setPrompt("카테고리를 선택하세요.");
		pouchCategory.setOnItemSelectedListener(this);
		
		product_selection = (ImageView) findViewById(R.id.product_selection);
		product_selection.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		product_selection.setOnClickListener(this);
		
		productImg1 = (ImageView) findViewById(R.id.productImg1);
		productImg2 = (ImageView) findViewById(R.id.productImg2);
		productImg3 = (ImageView) findViewById(R.id.productImg3);
		productImg4 = (ImageView) findViewById(R.id.productImg4);
		productImg5 = (ImageView) findViewById(R.id.productImg5);
		productImg6 = (ImageView) findViewById(R.id.productImg6);
		productImg7 = (ImageView) findViewById(R.id.productImg7);
		productImg8 = (ImageView) findViewById(R.id.productImg8);
		productImg9 = (ImageView) findViewById(R.id.productImg9);
		productImg10 = (ImageView) findViewById(R.id.productImg10);
		
		productImg1.setVisibility(View.INVISIBLE);
		productImg2.setVisibility(View.INVISIBLE);
		productImg3.setVisibility(View.INVISIBLE);
		productImg4.setVisibility(View.INVISIBLE);
		productImg5.setVisibility(View.INVISIBLE);
		productImg6.setVisibility(View.INVISIBLE);
		productImg7.setVisibility(View.INVISIBLE);
		productImg8.setVisibility(View.INVISIBLE);
		productImg9.setVisibility(View.INVISIBLE);
		productImg10.setVisibility(View.INVISIBLE);
		
		productImg1.setOnClickListener(this);
		productImg2.setOnClickListener(this);
		productImg3.setOnClickListener(this);
		productImg4.setOnClickListener(this);
		productImg5.setOnClickListener(this);
		productImg6.setOnClickListener(this);
		productImg7.setOnClickListener(this);
		productImg8.setOnClickListener(this);
		productImg9.setOnClickListener(this);
		productImg10.setOnClickListener(this);
		
		updateImg1 = (ImageView) findViewById(R.id.updateImg1);
		updateImg2 = (ImageView) findViewById(R.id.updateImg2);
		updateImg3 = (ImageView) findViewById(R.id.updateImg3);
		updateImg4 = (ImageView) findViewById(R.id.updateImg4);
		updateImg5 = (ImageView) findViewById(R.id.updateImg5);
		updateImg6 = (ImageView) findViewById(R.id.updateImg6);
		updateImg7 = (ImageView) findViewById(R.id.updateImg7);
		updateImg8 = (ImageView) findViewById(R.id.updateImg8);
		updateImg9 = (ImageView) findViewById(R.id.updateImg9);
		updateImg10 = (ImageView) findViewById(R.id.updateImg10);
		
		deleteImg1 = (ImageView) findViewById(R.id.deleteImg1);
		deleteImg2 = (ImageView) findViewById(R.id.deleteImg2);
		deleteImg3 = (ImageView) findViewById(R.id.deleteImg3);
		deleteImg4 = (ImageView) findViewById(R.id.deleteImg4);
		deleteImg5 = (ImageView) findViewById(R.id.deleteImg5);
		deleteImg6 = (ImageView) findViewById(R.id.deleteImg6);
		deleteImg7 = (ImageView) findViewById(R.id.deleteImg7);
		deleteImg8 = (ImageView) findViewById(R.id.deleteImg8);
		deleteImg9 = (ImageView) findViewById(R.id.deleteImg9);
		deleteImg10 = (ImageView) findViewById(R.id.deleteImg10);
		
		deleteImg1.setOnClickListener(this);
		deleteImg2.setOnClickListener(this);
		deleteImg3.setOnClickListener(this);
		deleteImg4.setOnClickListener(this);
		deleteImg5.setOnClickListener(this);
		deleteImg6.setOnClickListener(this);
		deleteImg7.setOnClickListener(this);
		deleteImg8.setOnClickListener(this);
		deleteImg9.setOnClickListener(this);
		deleteImg10.setOnClickListener(this);
		
		deleteImg1.setVisibility(View.INVISIBLE);
		deleteImg2.setVisibility(View.INVISIBLE);
		deleteImg3.setVisibility(View.INVISIBLE);
		deleteImg4.setVisibility(View.INVISIBLE);
		deleteImg5.setVisibility(View.INVISIBLE);
		deleteImg6.setVisibility(View.INVISIBLE);
		deleteImg7.setVisibility(View.INVISIBLE);
		deleteImg8.setVisibility(View.INVISIBLE);
		deleteImg9.setVisibility(View.INVISIBLE);
		deleteImg10.setVisibility(View.INVISIBLE);
		
		pouchTitle = (EditText) findViewById(R.id.pouchTitle);
		pouchText = (EditText) findViewById(R.id.pouchText);
		
		okBt = (ImageView) findViewById(R.id.okBt);
		okBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		okBt.setOnClickListener(this);
		
		categoryUrl = getResources().getString(R.string.server_url) + "/glowmee/pouchCategoryXml.do";
		
		(new PouchCategoryWorker()).execute();
		
	}
	
	public class PouchCategoryWorker extends AsyncTask<Object, String, Object> {
		@Override
		protected void onPreExecute() {
			CDialog.showLoading(AddPouch.this);
		}
		
		@Override
		protected Object doInBackground(Object... params) {
			Object result = null;
			
			// XML 파서 생성
			XmlPullParser parser = XmlParser.getInstance().getParser(categoryUrl);
			
			// 통신 실패시 예외처리
			if (parser == null) {
				this.cancel(true);
				return null;
			}
			
			try {
				String tag = null;
				int type = parser.getEventType();

				/** 읽은 내용을 담기 위한 변수 */
				String pouchCategoryText = null;
				int count = 1;

				// 파싱 처리를 위한 반복 시작
				while (type != XmlPullParser.END_DOCUMENT) {
					// 읽은 태그의 이름
					tag = parser.getName();

					// 현재 읽고 있는 태그의 종류별로 분기
					switch (type) {
					case XmlPullParser.START_TAG:
						/** 원하는 종류의 시작태그를 만났을 때, 값을 추출 */
						if (tag.equals("totalArticleCount")) {
							parser.next();
							categoryArr = new String[Integer.parseInt(parser.getText()) + 1];
							categoryArr[0] = "카테고리선택";
						} else if (tag.equals("pouchCategoryText")) {
							parser.next();
							pouchCategoryText = parser.getText();
						}
						
						break;

					case XmlPullParser.END_TAG:
						if (tag.equals("item")) {
							categoryArr[count] = pouchCategoryText;
							count++;
						}
						break;
					}

					// 다음 태그로 이동하고, 상태값을 변경
					type = parser.next();
				}

			} catch (XmlPullParserException e) {
				System.out.println("파서 생성 실패 >> " + e.toString());
			} catch (IOException e) {
				System.out.println("파서 읽기 실패 >> " + e.toString());
			}
			return result;
		}

		@Override
		protected void onProgressUpdate(String... param) {
		}

		@Override
		protected void onPostExecute(Object param) {
			ArrayAdapter<String> pouchCategory_adapter = new ArrayAdapter<String>(AddPouch.this, android.R.layout.simple_spinner_item, categoryArr);
			pouchCategory_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			pouchCategory.setAdapter(pouchCategory_adapter);
			
			CDialog.hideLoading();
		}
		
		@Override
		protected void onCancelled() {
			CDialog.hideLoading();
			AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setMessage("일시적인 통신에러입니다.");
			alert.setCancelable(false);
			alert.setPositiveButton("다시 시도", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					(new PouchCategoryWorker()).execute();
				}
			});
			alert.setNegativeButton("뒤로 가기", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			alert.create();
			alert.show();
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch(arg0.getId()) {
		case R.id.pouchCategory:
			
			if (arg2 == 0) {
				categoryText = "";
			} else {
				categoryText = categoryArr[arg2].trim();
			}
			
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		
	}
	
	public class AddWorker extends AsyncTask<Object, AddItem, Object> {
		@Override
		protected void onPreExecute() {
			CDialog.showLoading(AddPouch.this);
		}

		@Override
		protected Object doInBackground(Object... params) {
			Object result = null;
			
			FileInfo[] files = {fileInfo1, fileInfo2, fileInfo3, fileInfo4, fileInfo5, fileInfo6, fileInfo7, fileInfo8, fileInfo9, fileInfo10};
			
			url = getResources().getString(R.string.server_url) +  "/glowmee/addPouchXml.do?" +
					"idRegister=" + UserInfo.getInstance().getUSER_ID() + "&category=" + categoryText + "&title=" + pouchTitleValue +
					"&text=" + pouchTextValue + "&pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
					"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
					"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9] + "&t1=" + t1 + "&t2=" + t2 + 
					"&t3=" + t3 + "&t4=" + t4 + "&t5=" + t5 + "&t6=" + t6 + "&t7=" + t7 + "&t8=" + t8 + "&t9=" + t9 + "&t10=" + t10;
			
			// XML 파서 생성
			XmlPullParser parser = XmlParser.getInstance().getParserMulti(url, files);
			
			// 통신 실패시 예외처리
			if (parser == null) {
				this.cancel(true);
				return null;
			}
			
			try {
				String tag = null;
				int type = parser.getEventType();
				
				/** 읽은 내용을 담기 위한 변수 */
				String rt = null;
				String rtmsg = null;
				int seq = 0;
				// 파싱 처리를 위한 반복 시작
				while (type != XmlPullParser.END_DOCUMENT) {
					// 읽은 태그의 이름
					tag = parser.getName();

					// 현재 읽고 있는 태그의 종류별로 분기
					switch (type) {
					case XmlPullParser.START_TAG:
						/** 원하는 종류의 시작태그를 만났을 때, 값을 추출 */
						if (tag.equals("rt")) {
							parser.next();
							rt = parser.getText();
						} else if (tag.equals("rtmsg")) {
							parser.next();
							rtmsg = parser.getText();
						} else if (tag.equals("seq")) {
							parser.next();
							seq = Integer.parseInt(parser.getText().trim());
						}
						
						break;

					case XmlPullParser.END_TAG:
						
						if (tag.equals("JSPglowmee")) {
							addIdPouch = seq;
							AddItem item = new AddItem(rt, rtmsg, seq);
							publishProgress(item);
						}
						break;
					}

					// 다음 태그로 이동하고, 상태값을 변경
					type = parser.next();
				}

			} catch (XmlPullParserException e) {
				System.out.println("파서 생성 실패 >> " + e.toString());
			} catch (IOException e) {
				System.out.println("파서 읽기 실패 >> " + e.toString());
			}
			
			return result;
		}

		@Override
		protected void onProgressUpdate(AddItem... param) {
			
			if (param[0].getRt().equals("SUCCESS")) {
				AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
				alert.setTitle("추가 성공");
				alert.setMessage(param[0].getRtmsg());
				alert.setCancelable(false);
				alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						(new PouchWorker()).execute();
					}
				});
				alert.create();
				alert.show();
			} else {
				AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
				alert.setTitle("추가 실패");
				alert.setMessage(param[0].getRtmsg());
				alert.setCancelable(false);
				alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
				alert.create();
				alert.show();
			}
			
		}

		@Override
		protected void onPostExecute(Object param) {
			CDialog.hideLoading();
		}

		@Override
		protected void onCancelled() {
			CDialog.hideLoading();
			AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setMessage("일시적인 통신에러입니다.");
			alert.setCancelable(false);
			alert.setPositiveButton("다시 시도", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					(new AddWorker()).execute();
				}
			});
			alert.setNegativeButton("뒤로 가기", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			alert.create();
			alert.show();
		}
	}
	
	public class PouchWorker extends AsyncTask<Object, PouchCMD, Object> {
		@Override
		protected void onPreExecute() {
			CDialog.showLoading(AddPouch.this);
		}

		@Override
		protected Object doInBackground(Object... params) {
			Object result = null;
			
			String url = getResources().getString(R.string.server_url) + "/glowmee/listPickXml.do?" +
							"page=" + 1 + "&count=" + 1 + "&addIdPouch" + addIdPouch;
			
			XmlPullParser parser = XmlParser.getInstance().getParser(url);
			if (parser == null) {
				this.cancel(true);
				return null;
			}
			
			try {
				String tag = null;
				int type = parser.getEventType();

				/** 읽은 내용을 담기 위한 변수 */
				int idPouch = 0;
				String idPouchCategoryValue = null;
				int idBrand = 0;
				String idBrandValue = null;
				String pouchTitle = null;
				String pouchText = null;
				int editerPick = 0;
				String create_date = null;
				int idRegister = 0;
				String idRegisterValue = null;
				String userType = null;
				int birthYear = 0;
				String skinType = null;
				int productCount = 0;
				int recommendCount = 0;
				int commentCount = 0;
				String thumbnail = null;
				String user_thumbnail = null;
				String brand_thumbnail = null;
				
				// 파싱 처리를 위한 반복 시작
				while (type != XmlPullParser.END_DOCUMENT) {
					// 읽은 태그의 이름
					tag = parser.getName();

					// 현재 읽고 있는 태그의 종류별로 분기
					switch (type) {
					case XmlPullParser.START_TAG:
						/** 원하는 종류의 시작태그를 만났을 때, 값을 추출 */
						if (tag.equals("idPouch")) {
							parser.next();
							idPouch = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("idPouchCategoryValue")) {
							parser.next();
							idPouchCategoryValue = parser.getText();
						} else if (tag.equals("idBrand")) {
							parser.next();
							idBrand = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("idBrandValue")) {
							parser.next();
							idBrandValue = parser.getText();
						} else if (tag.equals("pouchTitle")) {
							parser.next();
							pouchTitle = parser.getText();
						} else if (tag.equals("pouchText")) {
							parser.next();
							pouchText = parser.getText();
						} else if (tag.equals("editerPick")) {
							parser.next();
							editerPick = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("create_date")) {
							parser.next();
							create_date = parser.getText();
						} else if (tag.equals("idRegister")) {
							parser.next();
							idRegister = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("idRegisterValue")) {
							parser.next();
							idRegisterValue = parser.getText();
						} else if (tag.equals("userType")) {
							parser.next();
							userType = parser.getText();
						} else if (tag.equals("birthYear")) {
							parser.next();
							birthYear = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("skinType")) {
							parser.next();
							skinType = parser.getText();
						} else if (tag.equals("productCount")) {
							parser.next();
							productCount = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("recommendCount")) {
							parser.next();
							recommendCount = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("commentCount")) {
							parser.next();
							commentCount = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("thumbnail")) {
							parser.next();
							thumbnail = parser.getText();
						} else if (tag.equals("user_thumbnail")) {
							parser.next();
							user_thumbnail = parser.getText();
						} else if (tag.equals("brand_thumbnail")) {
							parser.next();
							brand_thumbnail = parser.getText();
						}
						
						break;

					case XmlPullParser.END_TAG:
						
						if (tag.equals("item")) {
							pouchItem = new PouchCMD(idPouch, idRegister, idRegisterValue, userType, idPouchCategoryValue, 
									pouchTitle, pouchText, recommendCount, editerPick, create_date, thumbnail, user_thumbnail, brand_thumbnail, 
									productCount, commentCount, idBrand, idBrandValue, birthYear, skinType);
						}
						break;
					}

					// 다음 태그로 이동하고, 상태값을 변경
					type = parser.next();
				}

			} catch (XmlPullParserException e) {
				System.out.println("파서 생성 실패 >> " + e.toString());
			} catch (IOException e) {
				System.out.println("파서 읽기 실패 >> " + e.toString());
			}
			
			return result;
		}

		@Override
		protected void onProgressUpdate(PouchCMD... param) {
		}

		@Override
		protected void onPostExecute(Object param) {
			Intent intent = new Intent(AddPouch.this, ViewPouch.class);
			intent.putExtra("PARAM_paramId", pouchItem.getIdPouch());
			intent.putExtra("PARAM_isAdd", true);
			startActivityForResult(intent, 11);
			CDialog.hideLoading();
		}
		
		@Override
		protected void onCancelled() {
			CDialog.hideLoading();
			AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setMessage("일시적인 통신에러입니다.");
			alert.setCancelable(false);
			alert.setPositiveButton("다시 시도", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					(new PouchWorker()).execute();
				}
			});
			alert.setNegativeButton("뒤로 가기", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			alert.create();
			alert.show();
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.okBt:
			if (categoryText.equals("")) {
				Toast.makeText(this, "카테고리를 선택하세요.", Toast.LENGTH_SHORT).show();
				return;
			}
			
			pouchTitleValue = pouchTitle.getText().toString().trim();
			if (pouchTitleValue.equals("")) {
				Toast.makeText(this, "제목을 입력하세요.", Toast.LENGTH_SHORT).show();
				return;
			}
			
			pouchTextValue = pouchText.getText().toString().trim();
			if (pouchTextValue.equals("")) {
				Toast.makeText(this, "내용을 입력하세요.", Toast.LENGTH_SHORT).show();
				return;
			}
			
			int arrCount = 0;
			for (int i=0; i<productArr.length; i++) {
				arrCount += productArr[i];
			}
			if (arrCount == 0) {
				Toast.makeText(this, "제품을 1개이상은 등록해 주세요.", Toast.LENGTH_SHORT).show();
				return;
			}
			
			(new AddWorker()).execute();
			
			break;
			
		case R.id.productImg1:
			deleteImg1.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg2:
			deleteImg2.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg3:
			deleteImg3.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg4:
			deleteImg4.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg5:
			deleteImg5.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg6:
			deleteImg6.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg7:
			deleteImg7.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg8:
			deleteImg8.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg9:
			deleteImg9.setVisibility(View.VISIBLE);
			break;
			
		case R.id.productImg10:
			deleteImg10.setVisibility(View.VISIBLE);
			break;
			
		case R.id.deleteImg1:
			AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[0] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg1.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg2:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[1] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg2.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg3:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[2] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg3.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg4:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[3] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg4.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg5:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[4] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg5.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg6:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[5] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg6.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg7:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[6] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg7.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg8:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[7] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg8.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg9:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[8] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg9.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.deleteImg10:
			alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setTitle("제품 삭제");
			alert.setMessage("선택하신 제품을 삭제하시겠습니까?");
			alert.setCancelable(false);
			alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					pouchCount--;
					
					productArr[9] = 0;
					
					imgUrl = getResources().getString(R.string.server_url) + "/glowmee/listProductImgXml.do?" +
						"pro1=" + productArr[0] + "&pro2=" + productArr[1] + "&pro3=" + productArr[2] + 
						"&pro4=" + productArr[3] + "&pro5=" + productArr[4] + "&pro6=" + productArr[5] + "&pro7=" + productArr[6] +
						"&pro8=" + productArr[7] + "&pro9=" + productArr[8] + "&pro10=" + productArr[9];
					
					for (int i=0; i<productArr.length; i++) {
						productArr[i] = 0;
					}
							
					refreshCount = 1;
					(new RefreshWorker()).execute();
					
					empty();
				}
			});
			alert.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					deleteImg10.setVisibility(View.INVISIBLE);
				}
			});
			alert.create();
			alert.show();
			break;
			
		case R.id.product_selection:
			Intent update = new Intent(this, SearchProduct.class);
			update.putExtra("PARAM_pouchCount", pouchCount);
			update.putExtra("PARAM_productArr", productArr);
			startActivityForResult(update, 99);
			break;
		}
		
	}
	
	void empty() {
		productImg1.setVisibility(View.INVISIBLE);
		deleteImg1.setVisibility(View.INVISIBLE);
		updateImg1.setVisibility(View.VISIBLE);
		
		productImg2.setVisibility(View.INVISIBLE);
		deleteImg2.setVisibility(View.INVISIBLE);
		updateImg2.setVisibility(View.VISIBLE);
		
		productImg3.setVisibility(View.INVISIBLE);
		deleteImg3.setVisibility(View.INVISIBLE);
		updateImg3.setVisibility(View.VISIBLE);
		
		productImg4.setVisibility(View.INVISIBLE);
		deleteImg4.setVisibility(View.INVISIBLE);
		updateImg4.setVisibility(View.VISIBLE);
		
		productImg5.setVisibility(View.INVISIBLE);
		deleteImg5.setVisibility(View.INVISIBLE);
		updateImg5.setVisibility(View.VISIBLE);
		
		productImg6.setVisibility(View.INVISIBLE);
		deleteImg6.setVisibility(View.INVISIBLE);
		updateImg6.setVisibility(View.VISIBLE);
		
		productImg7.setVisibility(View.INVISIBLE);
		deleteImg7.setVisibility(View.INVISIBLE);
		updateImg7.setVisibility(View.VISIBLE);
		
		productImg8.setVisibility(View.INVISIBLE);
		deleteImg8.setVisibility(View.INVISIBLE);
		updateImg8.setVisibility(View.VISIBLE);
		
		productImg9.setVisibility(View.INVISIBLE);
		deleteImg9.setVisibility(View.INVISIBLE);
		updateImg9.setVisibility(View.VISIBLE);
		
		productImg10.setVisibility(View.INVISIBLE);
		deleteImg10.setVisibility(View.INVISIBLE);
		updateImg10.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 11:
			if (resultCode == Activity.RESULT_OK) {
				Intent intent = new Intent();
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
			break;
			
		case 99:
			if (resultCode == Activity.RESULT_OK) {
				@SuppressWarnings("unchecked")
				ArrayList<PouchProductCMD> productList = (ArrayList<PouchProductCMD>) data.getSerializableExtra("PARAM_productList");
				
				for (int i=0; i<productList.size(); i++) {
					PouchProductCMD cmd = productList.get(i);
					int idx = cmd.getIdProduct();
					String image = cmd.getImage();
					int temp = 0;
					if (pouchCount > 0) {
						temp = pouchCount + i;
					} else {
						temp = i;
					}
					switch (temp) {
					case 0:
						productArr[0] = idx;
						updateImg1.setVisibility(View.INVISIBLE);
						productImg1.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg1);
						break;
						
					case 1:
						productArr[1] = idx;
						updateImg2.setVisibility(View.INVISIBLE);
						productImg2.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg2);
						break;
						
					case 2:
						productArr[2] = idx;
						updateImg3.setVisibility(View.INVISIBLE);
						productImg3.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg3);
						break;
						
					case 3:
						productArr[3] = idx;
						updateImg4.setVisibility(View.INVISIBLE);
						productImg4.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg4);
						break;
						
					case 4:
						productArr[4] = idx;
						updateImg5.setVisibility(View.INVISIBLE);
						productImg5.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg5);
						break;
						
					case 5:
						productArr[5] = idx;
						updateImg6.setVisibility(View.INVISIBLE);
						productImg6.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg6);
						break;
						
					case 6:
						productArr[6] = idx;
						updateImg7.setVisibility(View.INVISIBLE);
						productImg7.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg7);
						break;
						
					case 7:
						productArr[7] = idx;
						updateImg8.setVisibility(View.INVISIBLE);
						productImg8.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg8);
						break;
						
					case 8:
						productArr[8] = idx;
						updateImg9.setVisibility(View.INVISIBLE);
						productImg9.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg9);
						break;
						
					case 9:
						productArr[9] = idx;
						updateImg10.setVisibility(View.INVISIBLE);
						productImg10.setVisibility(View.VISIBLE);
						ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + image.trim(), productImg10);
						break;
					}
				}
				
				pouchCount = pouchCount + productList.size();
			}
			break;
		}
	}
	
	public class RefreshWorker extends AsyncTask<Object, ProductCMD, Object> {
		@Override
		protected void onPreExecute() {
			CDialog.showLoading(AddPouch.this);
		}
		
		@Override
		protected Object doInBackground(Object... params) {
			Object result = null;
			
			// XML 파서 생성
			XmlPullParser parser = XmlParser.getInstance().getParser(imgUrl);
			
			// 통신 실패시 예외처리
			if (parser == null) {
				this.cancel(true);
				return null;
			}
			
			try {
				String tag = null;
				int type = parser.getEventType();

				/** 읽은 내용을 담기 위한 변수 */
				int idProduct = 0;
				String productThumbnail = null;

				// 파싱 처리를 위한 반복 시작
				while (type != XmlPullParser.END_DOCUMENT) {
					// 읽은 태그의 이름
					tag = parser.getName();

					// 현재 읽고 있는 태그의 종류별로 분기
					switch (type) {
					case XmlPullParser.START_TAG:
						/** 원하는 종류의 시작태그를 만났을 때, 값을 추출 */
						if (tag.equals("idProduct")) {
							parser.next();
							idProduct = Integer.parseInt(parser.getText().trim());
						} else if (tag.equals("productThumbnail")) {
							parser.next();
							productThumbnail = parser.getText();
						}
						
						break;

					case XmlPullParser.END_TAG:
						if (tag.equals("item")) {
							ProductCMD item = new ProductCMD(idProduct, productThumbnail);
							publishProgress(item);
						}
						break;
					}

					// 다음 태그로 이동하고, 상태값을 변경
					type = parser.next();
				}

			} catch (XmlPullParserException e) {
				System.out.println("파서 생성 실패 >> " + e.toString());
			} catch (IOException e) {
				System.out.println("파서 읽기 실패 >> " + e.toString());
			}
			return result;
		}

		@Override
		protected void onProgressUpdate(ProductCMD... param) {
			switch (refreshCount) {
			case 1:
				productArr[0] = param[0].getIdProduct();
				updateImg1.setVisibility(View.INVISIBLE);
				productImg1.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg1);
				break;
				
			case 2:
				productArr[1] = param[0].getIdProduct();
				updateImg2.setVisibility(View.INVISIBLE);
				productImg2.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg2);
				break;
				
			case 3:
				productArr[2] = param[0].getIdProduct();
				updateImg3.setVisibility(View.INVISIBLE);
				productImg3.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg3);
				break;
				
			case 4:
				productArr[3] = param[0].getIdProduct();
				updateImg4.setVisibility(View.INVISIBLE);
				productImg4.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg4);
				break;
				
			case 5:
				productArr[4] = param[0].getIdProduct();
				updateImg5.setVisibility(View.INVISIBLE);
				productImg5.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg5);
				break;
				
			case 6:
				productArr[5] = param[0].getIdProduct();
				updateImg6.setVisibility(View.INVISIBLE);
				productImg6.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg6);
				break;
				
			case 7:
				productArr[6] = param[0].getIdProduct();
				updateImg7.setVisibility(View.INVISIBLE);
				productImg7.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg7);
				break;
				
			case 8:
				productArr[7] = param[0].getIdProduct();
				updateImg8.setVisibility(View.INVISIBLE);
				productImg8.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg8);
				break;
				
			case 9:
				productArr[8] = param[0].getIdProduct();
				updateImg9.setVisibility(View.INVISIBLE);
				productImg9.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg9);
				break;
				
			case 10:
				productArr[9] = param[0].getIdProduct();
				updateImg10.setVisibility(View.INVISIBLE);
				productImg10.setVisibility(View.VISIBLE);
				ImageDownloader.getInstance().download(getResources().getString(R.string.server_url) + param[0].getImage().trim(), productImg10);
				break;
			}
			refreshCount++;
		}

		@Override
		protected void onPostExecute(Object param) {
			CDialog.hideLoading();
		}
		
		@Override
		protected void onCancelled() {
			CDialog.hideLoading();
			AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog));
			alert.setMessage("일시적인 통신에러입니다.");
			alert.setCancelable(false);
			alert.setPositiveButton("다시 시도", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					(new RefreshWorker()).execute();
				}
			});
			alert.setNegativeButton("뒤로 가기", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			alert.create();
			alert.show();
		}
	}
	
	// 하드웨어 뒤로가기버튼 이벤트 설정.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	switch (keyCode) {
    	//하드웨어 뒤로가기 버튼에 따른 이벤트 설정
		case KeyEvent.KEYCODE_BACK:
			new AlertDialog.Builder(new ContextThemeWrapper(AddPouch.this, android.R.style.Theme_Holo_Light_Dialog))
			.setTitle("픽 추가 취소")
			.setMessage("추가중인 픽이 모두 취소됩니다. 정말 뒤로 가시겠습니까?")
			.setPositiveButton("예", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			})
			.setNegativeButton("아니오", null).show();
			break;
		}
    	return super.onKeyDown(keyCode, event);
    }
    
    @Override
	protected void onDestroy() {
		super.onDestroy();
		am.removeActivity(this);
	}

}
